package controller;

import application.AtmFrame;

import domain.CardReaderImpl;
import domain.Receipt;
import domain.Withdrawl;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ResourceBundle;

public class ATMController implements Initializable {
    private Stage stage;

    private AtmFrame application;

    @FXML
    private TextField cardRead;

    @FXML
    private TextField CardId;

    @FXML
    private Button ok;

    @FXML
    private Text tip;

    @FXML
    private Text tips;
    @FXML
    private Text accountId;

    @FXML
    private TextField password;

    @FXML
    private Text actiontarget;

    @FXML
    private Button withdrawBt;

    @FXML
    private Button confirm;


    @FXML
    private Button exit;

    @FXML
    private TextArea cash;

    @FXML
    private TextField amount;

    @FXML
    private TextArea print;

    @FXML
    private Button back;

    @FXML
    private Button receipt;

    public void setApp(AtmFrame application) {
        this.application = application;

    }

    public ATMController(AtmFrame application) {
        this.application = application;
    }

    //插卡按钮

    //插卡

    public void pluginCard() {
        // 实例化card
        String cardId = CardId.getText();
        if (application != null) {
            application.pluginCard(cardId);
        } else {
            System.out.println("null");
        }
    }

    // 退卡
    public void exitCard() throws IOException {
        // 返回插卡页面
        application.exitCard();

    }

    public ATMController() {
    }


    //登录验证
    public void LoginAccount() throws IOException {

        String pword = password.getText();
        application.LoginAccount(pword);

    }

    //取款
    public void withdrawl() {

        application.withdrawl();

    }

    public void confirmWithdrawl() {
        String temp = amount.getText();
        application.confirmWithdrawl(temp);

    }

    //退回上一个界面
    public void back() {
        application.back();

    }

    public void setCash(String amount) {
        cash.setText("吐出" + amount + "元");
    }

    //打印按钮点击
    public void receiptPrint() {
        application.receiptPrint();
    }

    //打印
    public void print(Receipt receipt) {
        String result = "";

        if (receipt.getWithdrawlList().size() == 0) {
            result = "无取款记录";
        } else {
            result += "卡号：" + receipt.getWithdrawlList().get(0).getAccount().getCardId() + "\n";
            for (Withdrawl w : receipt.getWithdrawlList()) {
                result += " 取款时间：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(w.getDate());
                result += " 取款金额: " + w.getAmount();
                result += "\n";
            }
        }
        print.setText(result);


    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}
